package initialize

import (
	"github.com/samber/do/v2"

	"gitee.com/monkeyPP/happy-mall/pkg/db"
	"gitee.com/monkeyPP/happy-mall/pkg/es"
	"gitee.com/monkeyPP/happy-mall/pkg/redis"
	"gitee.com/monkeyPP/happy-mall/services/productservice/internal/application"
	"gitee.com/monkeyPP/happy-mall/services/productservice/internal/domain/service"
	"gitee.com/monkeyPP/happy-mall/services/productservice/internal/infrastructure/eventbus"
	httpServer "gitee.com/monkeyPP/happy-mall/services/productservice/internal/infrastructure/http"
	"gitee.com/monkeyPP/happy-mall/services/productservice/internal/infrastructure/persistence"
	"gitee.com/monkeyPP/happy-mall/services/productservice/internal/infrastructure/search"
	"gitee.com/monkeyPP/happy-mall/services/productservice/internal/infrastructure/server"
	"gitee.com/monkeyPP/happy-mall/services/productservice/internal/interfaces/api/handler"
)

var injector = do.New()

// Inject 依赖注入
func Inject() {
	// 创建 HTTP 服务器
	do.Provide(injector, httpServer.NewHTTPServerManager)
	do.Provide(injector, server.NewServerManager)

	do.Provide(injector, db.Init)
	do.Provide(injector, redis.Init)
	do.Provide(injector, es.Init)

	// product
	{
		do.Provide(injector, handler.NewProductHandler)
		do.Provide(injector, application.NewProductApp)
		do.Provide(injector, service.NewProductService)
		do.Provide(injector, persistence.NewProductRepository)
		do.Provide(injector, persistence.NewSkuRepository)
	}

	// eventbus
	{
		do.Provide(injector, eventbus.NewKafkaEventBus)
	}

	// es
	{
		do.Provide(injector, search.NewEsSearchService)
	}
}

func GetInjector() *do.RootScope {
	return injector
}
